(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  typeof define === 'function' && define.amd ? define(factory) :
  (global = global || self, global.formControl = factory());
}(this, (function () { 'use strict';

  function _createControl(ele, config) {
    this._ele = ele, this._config = config;

    this.render = function (config) {
      var _html = "";

      for (var j in config) {
        var key = config[j].type;
        var tips = "";

        if (config[j].description) {
          tips = '<i onclick="layer.tips(\'' + config[j].description + '\', this);" class="layui-icon layui-icon-tips"></i>';
        }

        if (key === "number" || key === "text") {
          _html += "<div class=\"layui-form-item\">\n              <label class=\"layui-form-label\">" + (config[j].name + tips) + "</label>\n              <div class=\"layui-input-block\">\n              <input type=\"text\" data-id='" + j + "' \n                  autocomplete=\"off\" \n                  class=\"layui-input\" \n                  name='" + config[j].name + "' value='" + config[j]["default"] + "'>\n              </div>\n              </div>\n              ";
        } else if (key === "checkbox") {
          _html += "<div class=\"layui-form-item\" >\n              <label class=\"layui-form-label\">" + (config[j].name + tips) + "</label>\n              <div class=\"layui-input-block\">\n              <input lay-filter='switchfilter' type=\"checkbox\" data-id='" + j + "' " + (config[j]["default"] == true ? "checked" : "") + " \n                name='" + j + "' lay-skin=\"switch\" lay-text=\"ON|OFF\" >\n                </div>\n                </div>\n              ";
        } else if (key === "singleitem") {
          var keyvalue = "";
          config[j]["default"].forEach(function (item) {
            keyvalue += item.value + '#' + item.label + '/';
          });
          keyvalue = keyvalue.substr(0, keyvalue.length - 1);
          _html += "<div class=\"layui-form-item\">\n              <label class=\"layui-form-label\">" + (config[j].name + tips) + "</label>\n              <div class=\"layui-input-block\">\n              <textarea type=\"text\" data-id='" + j + "' data-type=\"" + key + "\"\n                  autocomplete=\"off\" \n                  class=\"layui-textarea layui-input\" \n                  name='" + config[j].name + "' >" + keyvalue + "</textarea>\n              </div>\n              </div>\n              ";
        } else if (key === "columns") {
          var columns = config[j]["default"];
          var values = [];

          for (var i = 0; i < columns.length; i++) {
            values.push(columns[i].span);
          }

          _html += "<div class=\"layui-form-item\">\n              <label class=\"layui-form-label\">" + (config[j].name + tips) + "</label>\n              <div class=\"layui-input-block\">\n              <input type=\"text\" data-id='" + j + "' data-type=\"" + key + "\"\n                  autocomplete=\"off\" \n                  class=\"layui-input\" \n                  name='" + config[j].name + "' value='" + values + "'>\n              </div>\n              </div>\n              ";
        }
      }

      $(this._ele).html(_html);
      layui.form.render();
    };

    this.listenEvent = function (callback) {
      var that = this; //文本控件

      $(this._ele + ' .layui-input').off('keyup');
      $(this._ele + ' .layui-input').on('keyup', function (e) {
        if ($(this).attr("data-id") !== undefined) {
          var _key = $(this).attr("data-id");

          var _datatype = $(this).attr("data-type");

          var _value = $(this).val();

          if (_datatype == "singleitem") {
            var list = _value.split('/');

            _value = [];
            list.forEach(function (item) {
              _value.push({
                "value": item.split('#')[0],
                "label": item.split('#')[1]
              });
            });
          }

          if (_datatype == "columns") {
            var values = _value.split(",");

            var columns = that._config[_key]["default"];
            _value = [];

            for (var i = 0; i < values.length; i++) {
              var list = [];

              if (columns[i]) {
                list = columns[i].list;
              }

              _value.push({
                span: values[i],
                list: list
              });
            }
          }

          callback && callback(_key, _value);
        }
      });
      layui.form.on('switch(switchfilter)', function (data) {
        var _key = $(this).attr("data-id");

        var _value = data.elem.checked;
        callback && callback(_key, _value);
      });
    };

    this.getFormData = function () {};

    this.render(config);
    return this;
  }

  var formControl = {
    createControl: function createControl(ele, config) {
      return new _createControl(ele, config);
    }
  };

  return formControl;

})));
